<?php

namespace App\Http\Controllers\Admin\ExportOrigin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Admin\ItemDataController;
use App\Admin\SampleInfo;

class SichuanTongZhouController extends Controller
{
    /* 导出Excel 检测报告*/
    public function export(Request $request,$storeFile=''){
        $sampleId = $request->id;
        if($sampleId){ //如果是单样品原始记录直接导出
            return $this->exportRes();
        }
        set_time_limit(0);
        ini_set('max_execution_time', 0);
        ini_set('memory_limit', '-1');
        $temp = './storage/ExcelTemplate/原始记录.xls'; //考虑到兼容Linux，斜杠用“/”
        if (!file_exists($temp)) {
            return '原始记录模板文件不存在';
        }
        $contract_id = $request->contract_id;
        $batchNum = DB::table('a_contract')->where('id',$contract_id)->value('batchNum');
        $sampleIds = DB::table('a_sample_info as a')
            ->leftJoin('a_contract_gaiyao as b','a.contract_gaiyao_id','=','b.id')
            ->where('b.contract_id',$contract_id)
            ->orderBy('a.id','desc')
            ->pluck('a.bar_code','a.id')->toArray();
        $dir = "sampleOrigin/{$batchNum}";
        foreach ($sampleIds as $sampleId => $barCode) {
            $request->offsetSet('id',$sampleId);
            $this->exportRes("{$dir}/{$barCode}.xls");
        }

        $disk = Storage::disk('public');
        $spreadsheet = $spreadsheet2 = $worksheet =null;
        foreach ($sampleIds as $sampleId => $barCode) {
            $temp = $disk->path("{$dir}/{$barCode}.xls");
            if (!$spreadsheet) {
                $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($temp); //读取模板文件
            }else{
                $spreadsheet2 = \PhpOffice\PhpSpreadsheet\IOFactory::load($temp); //读取模板文件
                $worksheet2 = $spreadsheet2->getSheet(0);
                $spreadsheet->addExternalSheet($worksheet2,0); //添加外部工作表
                $spreadsheet2->disconnectWorksheets();
                unset($spreadsheet2);
            }
        }
        //----------直接在浏览器下载
        $file_name = "原始记录-批次{$batchNum}.xls"; //输出的文件名称
        $disk->deleteDirectory($dir);
        return self::saveDown($spreadsheet,$file_name,$storeFile);
    }

    //保存并下载
    public static function saveDown($spreadsheet,$file_name,$storeFile=''){
        //----------直接在浏览器下载----start
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        if ($storeFile) {
            $disk = Storage::disk('public');
            $disk->put($storeFile,''); //创建路径
            $writer->save($disk->path($storeFile));
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            return $disk->path($storeFile); 
        }
        
        // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出Excel 07文件
        header('Content-Type: application/vnd.ms-excel'); //告诉浏览器输出Excel 03版本文件
        header('Content-Disposition: attachment;filename=' . urlencode($file_name)); //告诉浏览器输出的文件名称
        header('Cache-Control: max-age=0'); //禁止缓存
        //----------直接在浏览器下载----end

        // $writer->save('write.xls');//将Excel文件保存在服务器的磁盘上
        $writer->setPreCalculateFormulas(false); //禁用公式预先计算
        $writer->save('php://output'); //直接在浏览器下载

        //为了防止内存泄露，建议用完手动清理
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;
    }

    /*导出结果（原始记录）*/
    public function exportRes($storeFile=''){
        $testdate_start = '/';
        $template_file = './storage/ExcelTemplate/原始记录.xls'; //考虑到兼容Linux，斜杠用“/”
        if (!file_exists($template_file)) {
            return '原始记录模板文件不存在';
        }
        $request = request();
        $id = $request->id;
        $info = DB::table('a_sample_info as a')
                ->leftJoin('a_contract_gaiyao as b','a.contract_gaiyao_id','=','b.id')
                ->leftJoin('a_contract as c','a.contract_id','=','c.id')
                ->leftJoin('a_status_time as d','a.contract_id','=','d.contract_id')
                ->where('a.id',$id)
                ->select('b.tool_name','a.bar_code','a.self_num','a.gqj_ID','b.std_num','b.voltage','b.class','a.sample_status','a.manufacturer','a.production_date','a.guige_xinghao','a.brand','a.remark','a.status0','a.tester_names','a.audit_man','c.audit_man as pi_man','c.batchNum','d.check_end','d.approve_end','b.class_id','b.vol_id','b.std_id','b.tool_id','a.contract_id','a.test_time')
                ->first();
        if(!$info){
            // dd(request()->all());
            return '样品不存在！';
        }
        //获取温湿度
        $temp_hum_pressure_data = DB::table('a_item_data as B')->select('temp', 'hum', 'pressure')->leftJoin('a_contract_gaiyao as A', 'A.id', '=', 'B.contract_gaiyao_id')->where('B.contract_id', $info->contract_id)->where('item_id', '!=', 61)->where('std_id', $info->std_id)->where('tool_id', $info->tool_id)->where('res', '<', 2)->get();
        $export = new \App\Http\Controllers\Admin\ExportReport\ExportReportPublicController();
        $se = $export::tempHumPressureSE($temp_hum_pressure_data);
        [$temp, $hum, $pressure] = $se;
        //检测日期
        if ($info->test_time) {
            $testdate_start = strtotime($info->test_time); //将任何字符串的日期时间描述解析为 Unix 时间戳
            $testdate_start = date("Y年m月d日", $testdate_start);
        }
        //检测仪器
        $dev_array = [];
        $dev_str = '';
        $dev_data = DB::table('a_item_data')->select('id','station_ids')->where('sample_info_id',$id)->where('station_ids','!=','')->get();
        if($dev_data) {
            $y = 0;
            foreach ($dev_data as $ke => $va) {
                $new_arr = explode(',',$va->station_ids);
                foreach($new_arr as $yv){
                    $dev_result = DB::table('a_station')->select('name','spec','fac_num')->where('id',$yv)->first();
                    if($dev_result) {
                        $dev_array[$y]['dev_name'] = $dev_result->name;
                        $dev_array[$y]['dev_num'] = $dev_result->fac_num;
                        $y++;
                    }
                }
            }
            //去重
            $dev_array = $this->array_duplicate($dev_array);
            if($dev_array){
                foreach($dev_array as $kkk=>$vvv){
                    $dev_str .= '设备名称：' . $vvv['dev_name'] . '，设备编号：' . $vvv['dev_num'] . '；';
                }
            }
        }

        $info->cycle = $this->get_cycle($info).'年';
        if(strpos($info->class,'t')){
            $class = $info->class;
        }else{
            $class = '/';
        }

        $baseInfo = [
            'A1' => $info->tool_name.'原始记录表',
            'C2' => $info->batchNum,
            'M2' => $info->tool_name,
            'C5' => $info->std_num,
            'C6' => $info->voltage,
            'M6' => $class,
            'C7' => $temp,
            'K7' => $hum,
            'N7' => $pressure,
            'M3' => $info->sample_status,
            'C3' => $info->bar_code,
            'C4' => $info->guige_xinghao,
            'M4' => $testdate_start,
            'M5' => $info->cycle,
            'C8' => $info->tester_names,
            'M8' => $info->audit_man,
            'C9' => $dev_str,
        ];
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($template_file); //读取模板文件
        $worksheet = $spreadsheet->getSheet(0); //按索引获取表单，从 0 开始
        foreach ($baseInfo as $cell => $value) {
            $worksheet->setCellValue($cell,$value);
        }
        $itemData = DB::table('a_item_data as a')
                    ->leftJoin('a_testitem as b','a.item_id','=','b.id')
                    ->leftJoin('manager as c','a.manager_id','=','c.id')
                    ->where('a.sample_info_id',$id)
                    ->select('b.name as item_name','c.realname as realname','a.utime','a.remark','a.item_id','a.res')
                    ->get();
        $rowIndex = $initRowIndex = 12;
        $initHeight = $worksheet->getRowDimension($rowIndex)->getRowHeight();
        $mergecells = [];
        $allMerge = $worksheet->getMergeCells();
        foreach ($allMerge as $mergecell) {
            if(strstr($mergecell, $rowIndex.':')){
                $mergecells[] = $mergecell;
            }
        }
        // dd($allMerge,$mergecells);
        foreach ($itemData as $k=>$item) {
            if ($k > 0) {
                $worksheet->insertNewRowBefore($rowIndex,1);
                foreach ($mergecells as $mergecell) {
                    $worksheet->mergeCells(str_replace($initRowIndex,$rowIndex,$mergecell));
                }
            }
            $worksheet->setCellValue('A'.$rowIndex,$item->item_name);
            //$worksheet->setCellValue('C'.$rowIndex,$item->realname);
            //$utime = $item->utime ? date('Y年m月d日',strtotime($item->utime)) : '';
            //$worksheet->setCellValue('E'.$rowIndex,$utime);
            /***************************检验标准  start******************************/
            $yaoqiu = DB::table('a_std_tool_item_rel')->where([
                    ['std_id','=',$info->std_id],
                    ['tool_id','=',$info->tool_id],
                    ['testitem_id','=',$item->item_id]
                ])->value('jishu_yaoqiu');
            $hegejielun = DB::table('a_std_tool_item_rel')->where([
                ['std_id','=',$info->std_id],
                ['tool_id','=',$info->tool_id],
                ['testitem_id','=',$item->item_id]
            ])->value('hege_jielun') ?:'符合';
            $key_condition = DB::table('a_condition_key')->select('id','key','unit')->where([
                    ['std_id','=',$info->std_id],
                    ['tool_id','=',$info->tool_id],
                    ['item_id','=',$item->item_id]
                ])->orderBy('sort')->get();
            $std_content = '';
            foreach ($key_condition as $k => $v){
                $val_condition = DB::table('a_condition')->where('condition_key_id','=',$v->id)->whereIn('vol_class_id',[0,$info->vol_id,$info->class_id])->pluck('val')->toArray();
                $val_condition = implode($val_condition,' / ');
                $std_content .= "{$v->key}：$val_condition{$v->unit}；";
            }
            $std_content = $item->item_id == 61 ? $yaoqiu : $std_content;
            $height = $this->toRowHeight($std_content,30,$initHeight);
            $worksheet->setCellValue('C'.$rowIndex,$std_content);
            /***************************检验标准  end******************************/
            /***************************原始记录  start******************************/
            $input_data = '';
            $key_data = DB::table('a_testdata_key')->select('id','data_key','data_unit')->where([
                ['std_id','=',$info->std_id],
                ['tool_id','=',$info->tool_id],
                ['item_id','=',$item->item_id]
            ])->orderBy('sort')->get();
            foreach ($key_data as $k => $v) {
                $input = DB::table('a_input_data')->where([
                                ['sample_info_id','=',$id],
                                ['testdata_key_id','=',$v->id]
                            ])->first();
                if ($input) {
                    $input_data_v= !empty($input->val) ? $input->val : "　";
                    $input_data .= "{$v->data_key}：{$input_data_v}{$v->data_unit}；";
                }
            }
            $height = $this->toRowHeight($yaoqiu,30,$height);
            $worksheet->setCellValue('J'.$rowIndex,$item->item_id == 61 ? '/' : $yaoqiu);
            /***************************原始记录  end******************************/
            if($item->res == 1){
                $worksheet->setCellValue('M' . $rowIndex, $item->item_id == 61 ? '外观检查符合检验标准' : $hegejielun );
            }else{
                $worksheet->setCellValue('M' . $rowIndex, $item->item_id == 61 ? '外观检查不符合检验标准' : $item->remark); //a_item_data表remark
            }
            
            //$worksheet->setCellValue('N'.$rowIndex,$item->res == 1 ? '合格' : ($item->res == 0 ? '不合格' : ''));
            $worksheet->setCellValue('P'.$rowIndex,'无');
            $worksheet->getRowDimension($rowIndex)->setRowHeight($height);
            $rowIndex += 1;
        }

        $worksheet->setTitle($info->tool_name.substr($info->bar_code,6));
        // dd($worksheet->getPageSetup());
        $styleArray = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ];
        if ($storeFile) {
            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
            $disk = Storage::disk('public');
            $disk->put($storeFile,''); //创建路径
            $writer->save($disk->path($storeFile));
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            return $disk->path($storeFile); 
        }else{
            $file_name = $info->tool_name.$info->bar_code.'.xls';
            return self::saveDown($spreadsheet,$file_name);
        }
    }

    //计算excel文字行高
    private function toRowHeight($str,$lineChrCount=13,$oldHeith=25){
        $length = (strlen($str)+mb_strlen($str,"UTF8"))/2;  //计算字符串长度（中文算2个字符，英文数字算一个字符）
        $toHeight = ceil($length/$lineChrCount)*16;
        return $toHeight > $oldHeith ? $toHeight : $oldHeith;
    }

    /**
     * 获取实验周期
     * @param $gaiyao_data
     * @return mixed
     */
    private function get_cycle($gaiyao_data){
        //获取检验周期
        //如果额定电压或者分类有试验周期
        $cycle = DB::table('a_voltage_class')->where("id", $gaiyao_data->class_id)->value('cycle'); //周期
        if (!$cycle) {
            $cycle = DB::table('a_voltage_class')->where("id", $gaiyao_data->vol_id)->value('cycle'); //周期
        }
        if (!$cycle){
            $where = [
                'std_id'=>$gaiyao_data->std_id,
                'tool_id'=>$gaiyao_data->tool_id,
            ];
            $cycle = DB::table('a_std_tool_rel')->where($where)->value('cycle'); //周期
        }
        info('工器具检测周期：'.$cycle);
        return $cycle;
    }

    //二维数据去重
    public function array_duplicate($arr){
        $tmp = array();
        $result = array();
        $result_arr = [];
        $i = 0;
        foreach($arr as $key => $val){
            $tmp[$key] = json_encode($val);
        }
        unset($val);
        $final = array_unique($tmp);
        foreach($final as $key => $val){
            $result[$key] = json_decode($val, true);
        }
        //序号重组
        foreach($result as $kk=>$vv){
            $result_arr[$i] = $vv;
            $i++;
        }

        return $result_arr;
    }

    /*批量导出样品结果*/
    /*public function exportBatchRes(Request $request){
        set_time_limit(0);
        ini_set('max_execution_time', 0);
        ini_set('memory_limit', '-1');
        $testdate_start = '/';
        $template_file = './storage/ExcelTemplate/原始记录.xls'; //考虑到兼容Linux，斜杠用“/”
        if (!file_exists($template_file)) {
            return '原始记录模板文件不存在';
        }
        $id = $request->id;
        $batchinfo = DB::table('a_sample_info as a')
            ->leftJoin('a_contract_gaiyao as b','a.contract_gaiyao_id','=','b.id')
            ->leftJoin('a_contract as c','a.contract_id','=','c.id')
            ->leftJoin('a_status_time as d','a.contract_id','=','d.contract_id')
            ->where('a.contract_id',$id)
            ->select('a.id','b.tool_name','a.bar_code','a.self_num','a.gqj_ID','b.std_num','b.voltage','b.class','a.sample_status','a.manufacturer','a.production_date','a.guige_xinghao','a.brand','a.remark','a.status0','a.tester_names','a.audit_man','c.audit_man as pi_man','c.batchNum','d.check_end','d.approve_end','b.class_id','b.vol_id','b.std_id','b.tool_id','a.contract_id','a.test_time')
            ->get();
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($template_file); //读取模板文件
        //循环样品添加sheet表格
        foreach($batchinfo as $key=>$info) {
            //
            if($key == 0) {
                $worksheet = $spreadsheet->getSheet(0); //按索引获取表单，从 0 开始
            }else{
                //========================== 复制表单 到 第一个模板文件========================
                $spreadsheet2 = \PhpOffice\PhpSpreadsheet\IOFactory::load($template_file); //读取模板文件
                $worksheet3 = $spreadsheet2->getSheet(0); //按索引获取表单，从 0 开始
                $worksheet3->setTitle($info->bar_code);
                $worksheet2 = clone $worksheet3;
                $spreadsheet->addExternalSheet($worksheet2); //添加外部工作表
                $spreadsheet2->disconnectWorksheets();
                unset($spreadsheet2);
                $worksheet = $spreadsheet->getSheetByName($info->bar_code); //按表单名 获取到激活的表单
            }
            //获取温湿度
            $temp_hum_pressure_data = DB::table('a_item_data as B')->select('temp', 'hum', 'pressure')->leftJoin('a_contract_gaiyao as A', 'A.id', '=', 'B.contract_gaiyao_id')->where('B.contract_id', $info->contract_id)->where('item_id', '!=', 61)->where('std_id', $info->std_id)->where('tool_id', $info->tool_id)->where('res', '<', 2)->get();
            $export = new \App\Http\Controllers\Admin\ExportReport\ExportReportPublicController();
            $se = $export::tempHumPressureSE($temp_hum_pressure_data);
            [$temp, $hum, $pressure] = $se;
            //检测日期
            if ($info->test_time) {
                $testdate_start = strtotime($info->test_time); //将任何字符串的日期时间描述解析为 Unix 时间戳
                $testdate_start = date("Y年m月d日", $testdate_start);
            }
            //检测仪器
            $dev_array = [];
            $dev_str = '';
            $dev_data = DB::table('a_item_data')->select('id','station_ids')->where('sample_info_id',$info->id)->where('station_ids','!=','')->get();
            if($dev_data) {
                $y = 0;
                foreach ($dev_data as $ke => $va) {
                    $new_arr = explode(',',$va->station_ids);
                    foreach($new_arr as $yv){
                        $dev_result = DB::table('a_station')->select('name','spec','fac_num')->where('id',$yv)->first();
                        if($dev_result) {
                            $dev_array[$y]['dev_name'] = $dev_result->name;
                            $dev_array[$y]['dev_num'] = $dev_result->fac_num;
                            $y++;
                        }
                    }
                }
                //去重
                $dev_array = $this->array_duplicate($dev_array);
                if($dev_array){
                    foreach($dev_array as $kkk=>$vvv){
                        $dev_str .= '设备名称：' . $vvv['dev_name'] . '，设备编号：' . $vvv['dev_num'] . '；';
                    }
                }
            }
            $info->cycle = $this->get_cycle($info).'年';
            if(strpos($info->class,'t')){
                $class = $info->class;
            }else{
                $class = '/';
            }
            $baseInfo = [
                'A1' => $info->tool_name.'原始记录表',
                'C2' => $info->batchNum,
                'M2' => $info->tool_name,
                'C5' => $info->std_num,
                'C6' => $info->voltage,
                'M6' => $class,
                'C7' => $temp,
                'K7' => $hum,
                'N7' => $pressure,
                'M3' => $info->sample_status,
                'C3' => $info->bar_code,
                'C4' => $info->guige_xinghao,
                'M4' => $testdate_start,
                'M5' => $info->cycle,
                'C8' => $info->tester_names,
                'M8' => $info->audit_man,
                'C9' => $dev_str,
            ];

            foreach ($baseInfo as $cell => $value) {
                $worksheet->setCellValue($cell,$value);
            }
            //具体检测项目
            $itemData = DB::table('a_item_data as a')
                ->leftJoin('a_testitem as b','a.item_id','=','b.id')
                ->leftJoin('manager as c','a.manager_id','=','c.id')
                ->where('a.sample_info_id',$info->id)
                ->select('b.name as item_name','c.realname as realname','a.utime','a.remark','a.item_id','a.res')
                ->get();
            $rowIndex = $initRowIndex = 12;
            $initHeight = $worksheet->getRowDimension($rowIndex)->getRowHeight();
            $mergecells = [];
            $allMerge = $worksheet->getMergeCells();
            foreach ($allMerge as $mergecell) {
                if(strstr($mergecell, $rowIndex.':')){
                    $mergecells[] = $mergecell;
                }
            }
            foreach ($itemData as $k=>$item) {
                if ($k > 0) {
                    $worksheet->insertNewRowBefore($rowIndex, 1); //在第x行之前插入1个新行
                    foreach ($mergecells as $mergecell) {
                        $worksheet->mergeCells(str_replace($initRowIndex,$rowIndex,$mergecell));
                    }
                }

                $worksheet->setCellValue('A'.$rowIndex,$item->item_name);
                //$worksheet->setCellValue('C'.$rowIndex,$item->realname);
                $utime = $item->utime ? date('Y年m月d日',strtotime($item->utime)) : '';
                //$worksheet->setCellValue('E'.$rowIndex,$utime);
                //***************************检验标准  start******************************
                $yaoqiu = DB::table('a_std_tool_item_rel')->where([
                    ['std_id','=',$info->std_id],
                    ['tool_id','=',$info->tool_id],
                    ['testitem_id','=',$item->item_id]
                ])->value('jishu_yaoqiu');
                $hegejielun = DB::table('a_std_tool_item_rel')->where([
                    ['std_id','=',$info->std_id],
                    ['tool_id','=',$info->tool_id],
                    ['testitem_id','=',$item->item_id]
                ])->value('hege_jielun');
                $key_condition = DB::table('a_condition_key')->select('id','key','unit')->where([
                    ['std_id','=',$info->std_id],
                    ['tool_id','=',$info->tool_id],
                    ['item_id','=',$item->item_id]
                ])->orderBy('sort')->get();
                $std_content = '';
                foreach ($key_condition as $k => $v){
                    $val_condition = DB::table('a_condition')->where('condition_key_id','=',$v->id)->whereIn('vol_class_id',[0,$info->vol_id,$info->class_id])->pluck('val')->toArray();
                    $val_condition = implode($val_condition,' / ');
                    $std_content .= "{$v->key}：$val_condition{$v->unit}；";
                }
                $std_content .=  "要求：$yaoqiu";
                $height = $this->toRowHeight($std_content,29,$initHeight);
                $worksheet->setCellValue('C'.$rowIndex,$std_content);
                //***************************检验标准  end*****************************
                //***************************原始记录  start*****************************
                $input_data = '';
                $key_data = DB::table('a_testdata_key')->select('id','data_key','data_unit')->where([
                    ['std_id','=',$info->std_id],
                    ['tool_id','=',$info->tool_id],
                    ['item_id','=',$item->item_id]
                ])->orderBy('sort')->get();
                foreach ($key_data as $k => $v) {
                    $input = DB::table('a_input_data')->where([
                        ['sample_info_id','=',$info->id],
                        ['testdata_key_id','=',$v->id]
                    ])->first();
                    if ($input) {
                        $input_data_v= !empty($input->val) ? $input->val : "　";
                        $input_data .= "{$v->data_key}：{$input_data_v}{$v->data_unit}；";
                    }
                }
                $height = $this->toRowHeight($input_data,29,$height);
                $worksheet->setCellValue('J' . $rowIndex, $item->item_id == 61 ? '/' : $input_data);
                //***************************原始记录  end*****************************
                if($item->res == 1){
                    $worksheet->setCellValue('M' . $rowIndex, $item->item_id == 61 ? '外观检查符合检验标准' : $hegejielun );
                }else{
                    $worksheet->setCellValue('M' . $rowIndex, $item->item_id == 61 ? '外观检查不符合检验标准' : $item->remark);   //a_item_data表中的remark字段
                }
                $worksheet->setCellValue('N'.$rowIndex,$item->res == 1 ? '合格' : ($item->res == 0 ? '不合格' : ''));
                $worksheet->setCellValue('P'.$rowIndex,'无');
                $worksheet->getRowDimension($rowIndex)->setRowHeight($height);
                $rowIndex += 1;
            }
            $worksheet->setTitle($info->tool_name.$info->bar_code);

        }

        $file_name = '原始记录'.$batchinfo[0]->batchNum.'.xls';
        header('Content-Type: application/vnd.ms-excel'); //告诉浏览器输出Excel 03版本文件
        header('Content-Disposition: attachment;filename='.urlencode($file_name));
        header('Cache-Control: max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        $writer->save('php://output');
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;
    }*/

}
